package com.link2room.soundwave.facade.impl;

import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.TryExceptionUtil;
import com.link2room.soundwave.dto.param.manager.L2RPMSCouponTypeManagerParamDto;
import com.link2room.soundwave.dto.result.base.L2RPmsCouponTypeDto;
import com.link2room.soundwave.entity.L2RPmsCouponType;
import com.link2room.soundwave.exception.SoundwaveException;
import com.link2room.soundwave.facade.IL2RPMSCouponTypeFacadeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Optional;

import static com.link2room.common.enums.BaseSysExceptionEnum.PARAM_IS_NULL;

/**
 * @author sqj88
 * @date 2018/1/5 14:23
 */
@Component("l2RPMSCouponTypeFacadeService")
public class L2RPMSCouponTypeFacadeServiceImpl implements IL2RPMSCouponTypeFacadeService {
    @Resource
    private ICommonService commonService;

    /**
     * 获取联房PMS电子券类型对照列表
     * @param baseL2rParamDto
     * @return
     * @throws Exception
     */
    @Override
    public QueryResult<L2RPmsCouponTypeDto> listL2RPMSCouponTypes(BaseL2rParamDto baseL2rParamDto)throws Exception{
        L2RPmsCouponType query=new L2RPmsCouponType();
        query.setLhotelGroupCode(baseL2rParamDto.getLhotelGroupCode());
        QueryResult<L2RPmsCouponType> l2RPmsCouponTypeQueryResult = commonService.findAllEQ(L2RPmsCouponType.class, query);
        return ClassConverUtil.copyQueryResultToDtoQueryResult(l2RPmsCouponTypeQueryResult,L2RPmsCouponTypeDto.class,false);
    }

    /**
     * 获取联房PMS电子券类型对照详情
     * @param baseL2rParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RPmsCouponTypeDto getL2RPMSCouponType(BaseL2rParamDto baseL2rParamDto)throws Exception{
        L2RPmsCouponType l2RPmsCouponType=Optional.ofNullable(baseL2rParamDto.getId()).map(
                TryExceptionUtil.of(value->{return commonService.findOneById(L2RPmsCouponType.class,value);})
        ).orElseThrow(()->new SoundwaveException(PARAM_IS_NULL,"id"));
        return ClassConverUtil.copyPropertiesToDto(l2RPmsCouponType,L2RPmsCouponTypeDto.class,false);
    }

    /**
     * 保存联房PMS电子券类型对照
     * @param l2RPMSCouponTypeManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RPmsCouponTypeDto saveL2RPMSCouponType(L2RPMSCouponTypeManagerParamDto l2RPMSCouponTypeManagerParamDto)throws Exception{
        return Optional.ofNullable(l2RPMSCouponTypeManagerParamDto.getL2RPmsCouponTypeDto()).map(
                TryExceptionUtil.of(l2RPmsCouponTypeDto->{
                    L2RPmsCouponType l2RPmsCouponType=ClassConverUtil.copyProperties(l2RPmsCouponTypeDto,L2RPmsCouponType.class);
                    commonService.save(l2RPmsCouponType);
                    l2RPmsCouponTypeDto.setId(l2RPmsCouponType.getId());
                    return l2RPmsCouponTypeDto;
                })
        ).orElseThrow(()->new SoundwaveException(BaseSysExceptionEnum.PARAM_IS_NULL,"l2RPmsCouponTypeDto"));
    }

    /**
     * 更新联房PMS电子券类型对照
     * @param l2RPMSCouponTypeManagerParamDto
     * @return
     * @throws Exception
     */
    @Override
    public L2RPmsCouponTypeDto updateL2RPMSCouponType(L2RPMSCouponTypeManagerParamDto l2RPMSCouponTypeManagerParamDto)throws Exception{
        return Optional.ofNullable(l2RPMSCouponTypeManagerParamDto.getL2RPmsCouponTypeDto()).map(
                TryExceptionUtil.of(l2RPmsCouponTypeDto->{
                    L2RPmsCouponType l2RPmsCouponType=ClassConverUtil.copyProperties(l2RPmsCouponTypeDto,L2RPmsCouponType.class);
                    commonService.merge(l2RPmsCouponType);
                    l2RPmsCouponTypeDto.setId(l2RPmsCouponType.getId());
                    return l2RPmsCouponTypeDto;
                })
        ).orElseThrow(()->new SoundwaveException(BaseSysExceptionEnum.PARAM_IS_NULL,"l2RPmsCouponTypeDto"));
    }
}
